package com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.matManage.vo.MatManageImportVo;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.repository.PersonnelManagementRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.vo.VehManageImportVo;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class VehManageExcelVerify implements IExcelVerifyHandler<VehManageImportVo> {
    @Resource
    private PersonnelManagementRepository personnelManagementRepository;
    @Resource
    EarEveManageService earEveManageService;


    @Override
    public ExcelVerifyHandlerResult verifyHandler(VehManageImportVo vehManageImportVo) {
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        StringBuilder message = new StringBuilder("");



        if (PlatformObjectUtils.isEmpty(vehManageImportVo.getVehicleNumber())) {
            result.setSuccess(false);
            message.append("车牌号 不能为空！");
        }else{
            if(vehManageImportVo.getVehicleNumber().length()>10){
                result.setSuccess(false);
                message.append("车牌号 不得超过10字！");
            }
        }

        if (PlatformObjectUtils.isEmpty(vehManageImportVo.getDriver())) {
            result.setSuccess(false);
            message.append("司机姓名 不能为空！");
        }
        if (PlatformObjectUtils.isEmpty(vehManageImportVo.getTel())) {
            result.setSuccess(false);
            message.append("司机联系方式 不能为空！");
        }else{
            // 定义手机号格式的正则表达式
            String regex = "^(1[0-9]{10})$";
            // 编译正则表达式
            Pattern pattern = Pattern.compile(regex);
            // 创建匹配器
            Matcher matcher = pattern.matcher(vehManageImportVo.getTel());
            if (!(matcher.matches())) {
                result.setSuccess(false);
                message.append("司机联系方式 格式不正确！");
            }else{
                EarEveManage currentEvent = earEveManageService.getCurrentEvent();
                PersonnelManagement person = personnelManagementRepository.findByXsemIdAndTelAndName(vehManageImportVo.getTel(), vehManageImportVo.getDriver(),currentEvent.getXsemId());
                //personnelManagementRepository.findByXsemIdAndTel();
                if(PlatformObjectUtils.isEmpty(person)){
                    result.setSuccess(false);
                    message.append("司机还未加入现场人员，无法导入！");
                }else{
                    vehManageImportVo.setDriverId(person.getXpimId());
                }
            }
        }
        if (PlatformObjectUtils.isNotEmpty(vehManageImportVo.getCommentInfo())) {
            if(vehManageImportVo.getCommentInfo().length()>500){
                result.setSuccess(false);
                message.append("车辆描述信息 不得超过500字！");
            }
        }









        result.setMsg(message.toString());
        return result;
    }

}
